﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using System.Data;
using TTSBLL;
using TTS;
using TTSModel;

public partial class T0002040_Add : System.Web.UI.Page
{
    private Assemble a;
    private UnitType u;
    private AssembleItem ai;
    private AssembleInfo ainfoanthor;
    private string message;
    private string T0002040_SN { get { return ViewState["T0002040_SN"].ToString(); } set { ViewState["T0002040_SN"] = value; } }
    private string T0002199_SN { get { return ViewState["T0002199_SN"].ToString(); } set { ViewState["T0002199_SN"] = value; } }
    private string T0002199_MAC_SN { get { return ViewState["T0002199_MAC_SN"].ToString(); } set { ViewState["T0002199_MAC_SN"] = value; } }
    private string T0001996Left_SN { get { return ViewState["T0001996Left_SN"].ToString(); } set { ViewState["T0001996Left_SN"] = value; } }
    private string T0001996Right_SN { get { return ViewState["T0001996Right_SN"].ToString(); } set { ViewState["T0001996Right_SN"] = value; } }

    private DataRow dr;
    private DataTable dt
    {
        get
        {
            return (DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }

    public T0002040_Add()
    {
        a = new Assemble();
        u = new UnitType();
        ai = new AssembleItem();
        ainfoanthor = new AssembleInfo();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            CommonHelper.AssembleItemDataTableInitialize(dt);
            this.T0002040_Version.Text = u.UnitType_GetbyTcode("T0002040").TcodeVersion;
            this.T0002199_Version.Text = u.UnitType_GetbyTcode("T0002199").TcodeVersion;
            this.T0001996_LeftVersion.Text = u.UnitType_GetbyTcode("T0001996").TcodeVersion;
            this.T0001996_RightVersion.Text = u.UnitType_GetbyTcode("T0001996").TcodeVersion;
        }
        this.TextBox_T0002040.Focus();
    }
    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("RRH.aspx");
    }
 /* protected void TextBox_T0002040_TextChanged(object sender, EventArgs e)
    {
        T0002040_SN = this.TextBox_T0002040.Text.Trim();
        if (SNHelper.SNCheck(T0002040_SN, "T0002040", out message))
        {
            if (SNHelper.SNCheckVersion(T0002040_SN, this.T0002040_Version.Text, out message))
            {
                if (ProConditionHelper.CheckSubSNsPrecondition(T0002040_SN, "T0002040", out message))
                {

                    this.TextBox_T0002040.Enabled = false;
                    this.TextBox_T0002199.Enabled = true;
                    this.TextBox_T0002199.Focus();
                    return;
                }
            }
            
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002040.Text = "";
    }*/
    protected void TextBox_T0002040_TextChanged(object sender, EventArgs e)
    {
        T0002040_SN = this.TextBox_T0002040.Text.Trim();
        if (SNHelper.SNCheck(T0002040_SN, "T0002040", out message))
        {
           // AssembleItem ai = new AssembleItem();
            // AssembleInfo ainfoanthor = a.Assemble_GetbySN(T0002040_SN);
            ainfoanthor = a.Assemble_GetbySN(T0002040_SN);
            if (string.IsNullOrEmpty(ainfoanthor.SN))
            {
                message = "T0002040的条码没有与防雷板装配过，无法进行当前装配！";
            }
            else
            {
                if (ai.AssembleItem_GetbyID(ainfoanthor.ID).Count == 1)
                {
                    this.TextBox_T0002040.Enabled = false;
                    this.TextBox_T0002199.Enabled = true;
                    this.TextBox_T0002199.Focus();
                    return;
                }
                else
                {
                    message = "T0002040的条码已经作为数字模块装配过，无法再次装配！";
                }
            }
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002040.Text = "";
    }
    protected void TextBox_T0002199_TextChanged(object sender, EventArgs e)
    {
        T0002199_SN = this.TextBox_T0002199.Text.Trim();
        if (SNHelper.SNCheck(T0002199_SN, "T0002199", out message))
        {
            if (SNHelper.SNCheckVersion(T0002199_SN, this.T0002199_Version.Text, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002199_SN, "T0002199", out message))
                {
                    if (ProConditionHelper.IsPassOrFail(T0002199_SN, "T0002199_GetStatusPro", out message))
                    {
                        this.TextBox_T0002199.Enabled = false;
                        this.TextBox_T0002199_MAC.Enabled = true;
                        this.TextBox_T0002199_MAC.Focus();
                        return;
                    }    
                }
            }           
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002199.Text = "";
        this.TextBox_T0002199.Focus();
    }
    protected void TextBox_T0002199_MAC_TextChanged(object sender, EventArgs e)
    {
        T0002199_MAC_SN = this.TextBox_T0002199_MAC.Text.Trim();

        if (SNHelper.SNMacCheck(T0002199_MAC_SN,out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002199_MAC_SN, "T0002199", out message))
                {
                    this.TextBox_T0002199_MAC.Enabled = false;
                    this.TextBox_T0001996Left.Enabled = true;
                    this.TextBox_T0001996Left.Focus();
                    return;
                }
            }                    
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002199_MAC.Text = "";
        this.TextBox_T0002199_MAC.Focus();
    }
    protected void TextBox_T0001996Left_TextChanged(object sender, EventArgs e)
    {
        T0001996Left_SN = this.TextBox_T0001996Left.Text.Trim();
        if (SNHelper.SNCheck(T0001996Left_SN, "T0001996", out message))
        {
            if (SNHelper.SNCheckVersion(T0001996Left_SN, this.T0001996_LeftVersion.Text, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0001996Left_SN, "T0001996", out message))
                {
                    if (ProConditionHelper.CheckSubSNsPrecondition(T0001996Left_SN, "T0001996", out message))
                    {
                        if (ProConditionHelper.IsPassOrFail(T0001996Left_SN, "T0001996_GetStatusPro", out message))
                        {
                            this.TextBox_T0001996Left.Enabled = false;
                            this.TextBox_T0001996Right.Enabled = true;
                            this.TextBox_T0001996Right.Focus();
                            return;
                        }              
                    }
                }
            }           
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0001996Left.Text = "";
        this.TextBox_T0001996Left.Focus();
    }
    protected void TextBox_T0001996Right_TextChanged(object sender, EventArgs e)
    {
        T0001996Right_SN = this.TextBox_T0001996Right.Text.Trim();
        if (T0001996Right_SN != T0001996Left_SN)
        {
            if (SNHelper.SNCheck(T0001996Right_SN, "T0001996", out message))
            {
                if (SNHelper.SNCheckVersion(T0001996Right_SN, this.T0001996_RightVersion.Text, out message))
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0001996Right_SN, "T0001996", out message))
                {
                    if (ProConditionHelper.CheckSubSNsPrecondition(T0001996Right_SN, "T0001996", out message))
                    {
                        if (ProConditionHelper.IsPassOrFail(T0001996Right_SN, "T0001996_GetStatusPro", out message))
                        {
                            this.TextBox_T0001996Right.Enabled = false;
                            this.ConfirmSubmit.Enabled = true;
                            this.ConfirmSubmit.Focus();
                            return;
                        }
                    }
                }
            }          
        }
        else
        {
            message = "T0001996电源板0的SN号与T0001996电源板1的SN号一致，请重新扫描！";            
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0001996Right.Text = "";
        this.TextBox_T0001996Right.Focus();
        
    }
    protected void ConfirmSubmit_Click(object sender, EventArgs e)
    {
        Unit u = new Unit();
        u.Insert(new UnitInfo() { SN = T0002040_SN, Tcode = "T0002040", IsLeaf = true, IsRoot = true });
        u.Insert(new UnitInfo() { SN = T0002199_SN, Tcode = "T0002199", IsLeaf = true, IsRoot = true });
        u.Insert(new UnitInfo() { SN = T0002199_MAC_SN, Tcode = "T0002199", IsLeaf = true, IsRoot = true });
        u.Insert(new UnitInfo() { SN = T0001996Left_SN, Tcode = "T0001996", IsLeaf = true, IsRoot = true });
        u.Insert(new UnitInfo() { SN = T0001996Right_SN, Tcode = "T0001996", IsLeaf = true, IsRoot = true });

        AssembleInfo ainfo = new AssembleInfo()
        {
            ID = 0,
            SN = T0002040_SN,
            IsRoot = true,
            RecordTime = DateTime.Now,
            Tcode = "T0002040",
            Userid = "111113"
        };

        dt.Rows.Clear();
        dr = dt.NewRow();
        dr["SubSN"] = T0002199_SN;
        dr["Remark"] = "RRH_digiboard";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["SubSN"] = T0002199_MAC_SN;
        dr["Remark"] = "RRH_digiboardMAC";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["SubSN"] = T0001996Left_SN;
        dr["Remark"] = "RRH_PowerPanel_0";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["SubSN"] = T0001996Right_SN;
        dr["Remark"] = "RRH_PowerPanel_1";
        dt.Rows.Add(dr);

       
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002199_SN, "T0002199", out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002199_MAC_SN, "T0002199", out message))
                {
                    if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0001996Left_SN, "T0001996", out message))
                    {
                        if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0001996Right_SN, "T0001996", out message))
                        {
                            if (a.Assemble_Insert(ainfo, dt))
                            {
                                message = "录入成功";
                                this.TextBox_T0002040.Text = "";
                                this.TextBox_T0002199.Text = "";
                                this.TextBox_T0002199_MAC.Text = "";
                                this.TextBox_T0001996Left.Text = "";
                                this.TextBox_T0001996Right.Text = "";
                                this.TextBox_T0002040.Enabled = true;
                                this.TextBox_T0002040.Focus();
                                this.ConfirmSubmit.Enabled = false;
                            }
                            else
                            {
                                message = "录入失败，请联系相关人员";
                            }
                        }
                    }
                }
            }
     
       
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
    }
}